﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using App_pressing_Loreau.Helper;
using App_pressing_Loreau.Data.DAO;
using App_pressing_Loreau.Model.DTO;
using System.Windows.Input;
using App_pressing_Loreau.Model;
using System.Windows;
using MySql.Data.MySqlClient;
using System.IO;


namespace App_pressing_Loreau.ViewModel
{

    /// <summary>
    /// Classe impression
    /// @param: Pour la méthode: 0: lecture x et 1:lecture Z
    /// Impression d un ticket lecture X en cours de journée et d un ticket lecture z pour la fin de journée
    /// </summary>
    class ImpressionVM : ObservableObject, IPageViewModel
    {
        string constring = "Server=localhost;Database=bddping1;Uid=root;Pwd=;";
        string filePath = "D:\\Application_Pressing\\Resources\\PatternFile";
        //String filePath ="D:\\perso\\test";
        String file = "";

        String msg = "";

        #region Constructeur
        public ImpressionVM()
        {
            file = filePath + "\\File.sql";
        }
        #endregion
        #region Properties and commands
        public String Name
        {
            get { return ""; }
        }

        public ICommand Btn_impression_imprimerTicketZ
        {
            get { return new RelayCommand(p => printTicketZ(1)); }
        }

        public ICommand Btn_impression_imprimerTicketX
        {
            get { return new RelayCommand(p => printTicketZ(0)); }
        }

        public ICommand Btn_esporter_bdd
        {
            get { return new RelayCommand(p => export()); }
        }

        #endregion

        #region Methods
        private void printTicketZ(int var)
        {
            try
            {
                LectureExcel le = new LectureExcel(var);
                le.printLecture();
            }
            catch (Exception e)
            {
                MessageBox.Show("Error" + e);
            }

        }

        private void export()
        {
         
            using (MySqlConnection conn = new MySqlConnection(constring))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        try
                        {
                            cmd.Connection = conn;
                            conn.Open();
                            mb.ExportToFile(file);
                            conn.Close();
                            msg += "Operation reussie" + Environment.NewLine;
                            msg += "retouver votre fichier dans le dossier:" + Environment.NewLine + filePath + Environment.NewLine;
                            
                        }
                        catch (Exception ex)
                        {
                            msg += ex + Environment.NewLine;
                        }

                        MessageBox.Show(msg);
                    }
                }
            }
        }

        
        
        #endregion
    }
}
